package cn.mw.cmdb.viewProcessor;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.entity.ModelGroupInfo;
import cn.mw.cmdb.processor.TableDataProcessorContext;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.ModelGroupServiceImpl;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.cmdb.view.StaticsViewInstance;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ModelGroupViewProcessor implements RelateDataViewProcessor<InstanceInfo, StaticsViewInstance>{

    private static final String ModelGroupKey = "modelGroup";

    private ViewProcessorManage viewProcessorManage;

    public ModelGroupViewProcessor(ViewProcessorManage viewProcessorManage){
        this.viewProcessorManage = viewProcessorManage;
    }

    @Override
    public void init(InstanceInfo instanceInfo, StaticsViewInstance staticsViewInstance) {
        String modelGroupId = instanceInfo.getGroupId();
        if(StringUtils.hasText(modelGroupId)){
            viewProcessorManage.addFieldIdSet(ModelGroupKey ,modelGroupId);
            staticsViewInstance.setModelGroupName(modelGroupId);
        }
    }

    @Override
    public void selectRelateData(TableDataProcessorContext context) throws Exception{
        CmdbServiceManage cmdbServiceManage = (CmdbServiceManage)context.get(CmdbServiceManage.class.getName());
        ModelGroupServiceImpl modelGroupService = (ModelGroupServiceImpl)cmdbServiceManage.getModelService(ModelGroupServiceImpl.ID);
        Set<String> groupIdSet = viewProcessorManage.getFieldIdSet(ModelGroupKey);
        if(null != groupIdSet && groupIdSet.size() > 0){
            List<String> groupIds = groupIdSet.stream().collect(Collectors.toList());
            SearchParam searchParam = new SearchParam();
            SubCondition subCondition = new SubCondition(CriteriaOpsType.in.name(), groupIds);
            searchParam.addSubCondition(CMDBTool.getFieldName(ModelGroupInfo::getId) ,subCondition);
            List<ModelGroupInfo> list = modelGroupService.selectList(searchParam);
            if(null != list){
                for(ModelGroupInfo modelGroupInfo: list){
                    viewProcessorManage.addFieldIdMap(ModelGroupKey ,modelGroupInfo.getId() ,modelGroupInfo.getName());
                }
            }
        }
    }

    @Override
    public void setRelateData(StaticsViewInstance staticsViewInstance) {
        String id = staticsViewInstance.getModelGroupName();
        Map<String, String> map = viewProcessorManage.getFieldIdMap(ModelGroupKey);
        if(StringUtils.hasText(id) && null != map){
            String name = map.get(id);
            if(StringUtils.hasText(name)){
                staticsViewInstance.setModelGroupName(name);
            }
        }
    }

    @Override
    public int hashCode() {
        return getClass().getSimpleName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return getClass().getSimpleName().equals(obj.getClass().getSimpleName());
    }
}
